import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter)

// 引入页面
import Home from '@/views/Home'
import Search from '@/views/Search'
import Login from '@/views/Login'
import Register from '@/views/Register'
import Detail from '@/views/Detail'
import AddCartSuccess from '@/views/AddCartSuccess'
import ShopCart from '@/views/ShopCart'

// 重写push和replace方法
let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace
VueRouter.prototype.push = function (location, reslove, reject) {
  if (reslove && reject) {
    originPush.call(this, location, reslove, reject)
  } else {
    originPush.call(this, location, () => { }, () => { })
  }
}
VueRouter.prototype.replace = function (location, reslove, reject) {
  if (reslove && reject) {
    originReplace.call(this, location, reslove, reject)
  } else {
    originReplace.call(this, location, () => { }, () => { })
  }
}

export default new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/home',
      meta: {
        show: true
      }
    },
    {
      path: '/home',
      component: Home,
      meta: {
        show: true
      },
      // 路由组件通过props传递数据
      // 布尔值写法只能传递params参数
      // props:true
      // 对象写法
      // props:{a:1,b:2}
      // 函数写法:prams和query都可以传，组件中通过props接收使用
      // props:($route)=>{
      //   return {
      //     keyword:$route.params.keyword,
      //     k:$route.query.keyword
      //   }
      // }
    },
    {
      name:'search',
      path: '/search/:keyword?',
      component: Search,
      meta: {
        show: true
      }
    },
    {
      path: '/login',
      component: Login,
      meta: {
        show: false
      }
    },
    {
      path: '/register',
      component: Register,
      meta: {
        show: false
      }
    },
    {
      path:'/detail/:skuId?',
      component:Detail,
      meta:{show:false}
    },
    {
      path:'/addcartsuccess',
      component:AddCartSuccess,
      meta:{show:true}
    },
    {
      path:'/shpocart',
      component:ShopCart,
      meta:{show:true}
    }
  ],
  scrollBehavior() {
    // 始终滚动到顶部
    return { y: 0 }
  },
})